﻿using System;
using System.Collections.Generic;
using System.ComponentModel.Composition;
using System.ComponentModel.Composition.Hosting;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace MEF_Second
{
    public class Configuration
    {
        [Export("Timeout")]
        public int Timeout
        { get { return 11; } }
    }

    [Export]
    public class UsesTimeout
    {
        [Import("Timeout")]
        public int Time { get; set; }
    }



    public class MessageSender
    {
        [Export(typeof(Action<string>))]
        public void Send(string message)
        {
            Console.WriteLine(message);
        }
    }

    [Export]
    public class Processor
    {
        [Import(typeof(Action<string>))]
        public Action<string> MessageSend { get; set; }

        public void Send()
        {
            MessageSend("Processed");
        }
    }



    [InheritedExport]
    public interface ILogger
    {
        void Log(string message);
    }
    public class Logger:ILogger
    {
        public void Log(string message)
        {
            Console.WriteLine(message);
        }
    }

    class Program
    {
        
        [Import]
        public UsesTimeout usetimeout { get; set; }

        [Import]
        public Processor processor { get; set; }

        [Import]
        public ILogger logger { get; set; }

        static void Main(string[] args)
        {
            Program p = new Program();
            p.Run();
            Console.ReadKey();
        }

        public void Run()
        {
            //利用目录添加到容器
            var catalog = new AssemblyCatalog(System.Reflection.Assembly.GetExecutingAssembly());
            var container = new CompositionContainer(catalog);
            container.ComposeParts(this);

            Console.WriteLine(usetimeout.Time);

            processor.Send();

            logger.Log("log");

        }

    }
}
